const db = require('../tool/db.js');
const common = require('../model/common.js')

async function add(data) {
    if (data.parentid == 1) {
        let [res] = await db.query('insert into coupon (`name`,parentid,price,`condition`,startTime,endTime) values(?,?,?,?,?,?)', [data.name, data.parentid, data.price, data.condition, data.startTime, data.endTime])
        if (res.insertId) {
            return {
                code: 1,
                msg: '添加成功',
                data: ''
            }
        }
    }
    if (data.parentid == 2) {
        let [res] = await db.query('insert into coupon (`name`,parentid,price,startTime,endTime) values(?,?,?,?,?)', [data.name, data.parentid, data.price, data.startTime, data.endTime])
        if (res.insertId) {
            return {
                code: 1,
                msg: '添加成功',
                data: ''
            }
        }
    }
}

async function list(data) {
    let sql = 'select * from coupon where parentid'
    let sql2 = 'select count(*) as total from coupon where parentid'
    switch (data.id) {
        case 1: //满减
            sqll = '= 1'
            break;
        case 2: //无门槛
            sqll = '= 2'
            break;
        case 3: //未过期
            sqll = '!= 0 and status = 1'
            break;
        case 4: //过期
            sqll = '!= 0 and status = 0'
            break;

        default:
            sqll = '!= 0'
            break;
    }
    let [res] = await db.query(sql + sqll + ' limit ?,?', [data.offset, data.num])
    let [[{total}]] = await db.query(sql2 + sqll)
    if (res.length) {
        return {
            code: 1,
            msg: '成功',
            data: res,
            total:total
        }
    }
}

async function mod(date) {
    let [couponList] = await db.query('select id,endTime from coupon where parentid != 0 and status = 1')
    for (const i of couponList) {
        if (i.endTime < date) {
            let res = await db.query('update coupon set status = 2 where id = ' + i.id)
        }
    }
    return {
        code: 1,
        msg: '成功',
        data: ''
    }
}

async function get(data) {
    let [usercoupon] = await db.query('select * from user_coupon where couponid = ? and uid = ?', [data.couponid, data.id])
    if (usercoupon.length) {
        return {
            code: 0,
            msg: '已经领取过了',
            data: ''
        }
    }
    let [res] = await db.query('insert into user_coupon (couponid,uid) values(?,?)', [data.couponid, data.id])
    if (res.insertId) {
        return {
            code: 1,
            msg: '领取成功',
            data: ''
        }
    }
}

async function userlist(data) {
    let sql = ''

    switch (data.id) {
        case 1: //可用
            sql = 'select id,`name`,`condition`,startTime as startAt,endTime as endAt,price as `value`,parentid,`status`,uid from coupon as b join (select couponid,uid from user_coupon where uid = ? and status = 1 ) as a on a.couponid = b.id and status = 1'
            break;
        case 2: //过期
            sql = 'select id,`name`,`condition`,startTime as startAt,endTime as endAt,price as `value`,parentid,`status`,uid from coupon as b join (select couponid,uid from user_coupon where uid = ? and status = 1 ) as a on a.couponid = b.id and status = 0'
            break;
        case 3: //历史
            sql = 'select id,`name`,`condition`,startTime as startAt,endTime as endAt,price as `value`,parentid,`status`,uid from coupon as b join (select couponid,uid from user_coupon where uid = ? and status = 2) as a on a.couponid = b.id'
            break;
        case 4: //回收站
            sql = 'select id,`name`,`condition`,startTime as startAt,endTime as endAt,price as `value`,parentid,`status`,uid from coupon as b join (select couponid,uid from user_coupon where uid = ? and status = 0) as a on a.couponid = b.id'
            break;
        default: 
            break;
    }
    let [res] = await db.query(sql, [data.uid])
    if (res.length) {
        return {code: 1,msg: '成功',data: res}
    }else{
        return {code: 0,msg: '没有更多了',data: res}
    }
    

}

async function del(data){
    let [res] = await db.query('update user_coupon set status = 1 - status where couponid = ? and uid = ?',[ data.id,data.uid])
    if(res.affectedRows){
        return {code:1,msg:'操作成功',data:''}
    }
}

module.exports = {
    add,
    list,
    mod,
    get,
    userlist,
    del
}